/*Created under the GNU General Public Liseance Project:Sinhala Synonyms Supporter Author S.N.S.Dewasurendra
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

/**
 *
 * @author User
 */

import Dict.GenaralDictionary;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/* This class makes dictionary and sores it in a data file*/
public class Data {

    private ArrayList<String> list=new ArrayList();                  //store dictionary data file(EN-SI TIP) input
    private String[] words= new String[2];
    private String path = Data.class.getProtectionDomain().getCodeSource().getLocation().getPath();// get the path of the dictionaty tree map object saving file
 
    private GenaralDictionary dictionary = new GenaralDictionary();

    /**Constructer for data class. creates the path to the dictionary file*/
    public Data() {
       System.out.print(path);
        path = path.replace("/", "\\");     //the forward slashes are replace by double back slashes to the path
        path = path.replaceFirst("\\\\", ""); //the first occurence of /// is removed
         path = path.replace("%20", " "); //replace the space
        path = path.replace("SinSynonyms.jar", "Text.txt");//The file name of the dictionary is added to the path by replacing the name of the jar file with the file name
          this.createDictionary();
    }

 
    

    public GenaralDictionary getDictionary() {
        return dictionary;
    }

    /** Returns the path of the dictionary object file*/
    public String getPath() {
        return path;
    }

    /**get the wordlist from ensi-tip dictionary*/
    public void readFile() throws IOException {

        FileInputStream fis = new FileInputStream(getPath());
        InputStreamReader isr = new InputStreamReader(fis, "UTF8");
        BufferedReader br = new BufferedReader(isr);
        String line;
        int counter = 0;


        while ((line = br.readLine()) != null) {       //while there are words

            list.add(line);
            counter++;
        }
   
        fis.close();


    }

    /**inserts the ensi tip data in the treemap*/
    public void makeDictionary() throws IOException {

        this.readFile();
        for (int i = 0; i < list.size(); i++) {


            if(list.get(i)==null)break;

            words = list.get(i).split("=");
            String word = words[0];
            String meaning = words[1];


            dictionary.insert(word, meaning);


        }


    }

   

    /** Does the total functionality to create the dictionary*/
    public void createDictionary() {
        try {

            this.makeDictionary();  //makes the dictionary


          //this.storeData();       //stores the dictionary
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Data.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Data.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

//    public static void main(String[] args) {
//        Data d = new Data();
//
//    }
}
